/* <NAVAGIS> */
var language = $("#hdnLang").val();
//alert(language);
// Remove this next line:
var _gaq; if (!_gaq) _gaq = new Array();
// End remove

var ge = null;
var baseHref = location.protocol + '//' + location.host + '/'; //location.pathname;
var hash = null;

if (window.location.hash) {
    hash = window.location.hash.substring(1);
    if (hash == "" || hash.substring(0, 1) == ".") {
        hash = null;
    }
}

// Current demo and info
var demo;
var currentSector;
var currentSectorNumber;

// Demo switching interface
var switchDemoUI;

// UI overlay
var jDemoUI;
var jDemoUIR;

var HASH_TO_DEMO = {
    "telco1": ["telco", 1],
    "telco2": ["telco", 2],
    "telco3": ["telco", 3],
    "utilities1": ["utilities", 1],
    "utilities2": ["utilities", 2],
    "resource1": ["resource", 1],
    "resource2": ["resource", 2],
    "government1": ["government", 1],
    "government2": ["government", 2],
    "financial1": ["financial", 1]
};

/* </NAVAGIS> */

google.load("maps", "3", { "other_params": "sensor=true" });
google.load("earth", "1");

function getImageLocation(path) {

    if (language == null || language == undefined) {
        return 'img/' + path;
    }
    else {
        return 'img/' + language + "/" + path;
    }

}

function loadDemo(sector) {
    // the parameter sector will be either "telco", "utilities", "resource", "government" or "financial"
    // please instantiate the demo into #demoholder and call startDemo when earth is loaded.

    // sample earth plugin
    /* <NAVAGIS> */
    if (ge == null) {
        createEarth(sector, 1);
    } else {
        loadSubDemo(sector, 1);
    }
    /* </NAVAGIS> */
}

function createEarth(sector, number) {
    google.earth.createInstance('demoholder', initCB, failureCB);
    currentSector = sector;
    currentSectorNumber = number;
}

function initCB(instance) {
    ge = instance;
    ge.getWindow().setVisibility(true);
    /* <NAVAGIS> */
    loadSubDemo(currentSector, currentSectorNumber);
    /* </NAVAGIS> */
}
function failureCB(errorCode) {
    $("#earth").hide();
    $(".earthshim").hide();

    var nopluginLine1 = demojson.noplugin.line1;
    var nopluginLine2 = demojson.noplugin.line2;

    $("#demoholder").html('<div class="MainHolder" style="font-family: OpenSansLight,Sans-Serif;font-size: 12px;line-height: 22px;height: 612px;width: 1146px;background: #d3d3d3 url(' +
			  baseHref + 'img/noPlugin/main_bg.jpg) top center no-repeat;overflow: hidden;">' +
			  '<div class="ContentBox" style = "height: 170px;width: 570px;margin: 221px auto 10px auto;text-align: center;background: transparent url(' + baseHref + 'img/noPlugin/content_box.png) 0 0 no-repeat;">' +
			  '<img src="' + getImageLocation("noPlugin/discover_title.png") + '" class="TitleImage" style="margin: 44px auto 16px auto;" /><br />' +

              '<a style="color: #dd4931;" target="_blank" href=" http://www.google.com/intl/en/earth/explore/products/plugin.html">' + nopluginLine1 + '</a>' + '<br/>' +
              '<a style="color: #dd4931;" target="_blank" href=" http://http://discovermapsforbusiness.com">' + nopluginLine2 + '</a>' + '<br/>' +
			  '</div>' +
			  '</div>');
}


/* <NAVAGIS> */
function isSwitchDemoOpen() {
    return ((switchDemoUI != null) && switchDemoUI.isOpen);
}

function loadFromHash() {
    if (hash != null) {
        for (var d in HASH_TO_DEMO) {
            if (hash == d) {
                if (ge == null) {
                    createEarth(HASH_TO_DEMO[d][0], HASH_TO_DEMO[d][1]);
                } else {
                    loadSubDemo(sector, 1);
                }
                return true;
            }
        }
    }
    return false;
}

function setHash(sector, number) {
    for (var d in HASH_TO_DEMO) {
        if (sector == HASH_TO_DEMO[d][0]) {
            if (number == HASH_TO_DEMO[d][1]) {
                window.location.hash = d;
                changeSharingURL(window.location);
                return;
            }
        }
    }
}

function loadSubDemo(sector, number) {
    // Initialize UI overlay.
    if (jDemoUI == null) {
        var y = $('#map').offset().top;
        var h = $('#map').height();
        jDemoUI = $('<div id="demoUI" style="top:' + y + 'px;height:' + h + 'px;">');
        jDemoUI.addClass('ui-widget');
        jDemoUI.addClass('demoUI');
        jDemoUI.hide();
        jDemoUI.appendTo('body');
        autoShimAttach(jDemoUI);

        var border = 16;
        jDemoUIR = $('<div id="demoUIR" style="top:' + border + 'px;max-height:' + (h - 2 * border) + 'px;z-index:2000;">');
        jDemoUIR.addClass('ui-widget');
        jDemoUIR.addClass('demoUI');
        jDemoUIR.hide();

        jDemoUIR.appendTo('body');

        autoShimAttach(jDemoUIR);
        autoPositionAttach(jDemoUIR, $('#map'), 96, 32);
    }

    // Track new demo info for presenting demo switch later.
    currentSector = sector;
    currentSectorNumber = number;

    setHash(sector, number);

    // Clear current demo
    if (demo != null) {
        demo.hide();
        ge.setBalloon(null);
        var features = ge.getFeatures();
        while (features.getLastChild() != null) {
            features.removeChild(features.getLastChild());
        }
    }

    if (isSwitchDemoOpen()) {
        switchDemoUI.hide();
    }

    // Decide which demo to launch.
    if (sector == "telco") {
        if (number == 1) {
            demo = new DemoTelco1();
            startDemo();
            _gaq.push(['_trackEvent', 'Telco - Demo Loaded', 'play', 'Loaded Telco Demo']);
        } else if (number == 2) {
            demo = new DemoTelco2();
            startDemo();
            _gaq.push(['_trackEvent', 'Telco2 - Demo Loaded', 'play', 'Loaded Telco2 Demo']);
        } else if (number == 3) {
            demo = new DemoTelco3();
            startDemo();
            _gaq.push(['_trackEvent', 'Telco3 - Demo Loaded', 'play', 'Loaded Telco3 Demo']);
        }
    } else if (sector == "utilities") {
        if (number == 1) {
            demo = new DemoUtilities1();
            startDemo();
            _gaq.push(['_trackEvent', 'Utilities - Demo Loaded', 'play', 'Loaded Utilities Demo']);
        } else if (number == 2) {
            demo = new DemoUtilities2();
            startDemo();
            _gaq.push(['_trackEvent', 'Utilities2 - Demo Loaded', 'play', 'Loaded Utilities2 Demo']);
        }
    } else if (sector == "resource") {
        if (number == 1) {
            demo = new DemoResource1();
            startDemo();
            _gaq.push(['_trackEvent', 'Resource - Demo Loaded', 'play', 'Loaded Resource Demo']);
        } else if (number == 2) {
            demo = new DemoResource2();
            startDemo();
            _gaq.push(['_trackEvent', 'Resource2 - Demo Loaded', 'play', 'Loaded Resource2 Demo']);
        }
    } else if (sector == "government") {
        if (number == 1) {
            demo = new DemoGovernment1();
            startDemo();
            _gaq.push(['_trackEvent', 'Government - Demo Loaded', 'play', 'Loaded Government Demo']);
        } else if (number == 2) {
            demo = new DemoGovernment2();
            startDemo();
            _gaq.push(['_trackEvent', 'Government2 - Demo Loaded', 'play', 'Loaded Government2 Demo']);
        }
    } else if (sector == "financial") {
        if (number == 1) {
            demo = new DemoFinancial1();
            startDemo();
            _gaq.push(['_trackEvent', 'Financial - Demo Loaded', 'play', 'Loaded Financial Demo']);
        } else if (number == 2) {
        }
    }
}

function startDemo() {
    //$(".earthshim, #earth").remove();

    $("#earth").hide();
    $(".earthshim").hide();

    demo.init();
}

function restartDemo() {
    if ((currentSector != null) && (currentSectorNumber != null)) {
        loadSubDemo(currentSector, currentSectorNumber);
    }
}

function switchDemo() {
    if (demo != null) {
        demo.hide();
    }

    if (switchDemoUI == null) {
        switchDemoUI = new SwitchDemo();
        switchDemoUI.init();
        switchDemoUI.hide();
    }
    showSwitchDemoUI();
}

function showSwitchDemoUI() {
    $("#earth").show();
    $(".earthshim").show();

    switchDemoUI.setDemo(currentSector, currentSectorNumber);
    switchDemoUI.show();
}

/* </NAVAGIS> */

/* <SPUTNIK Change sharing URL> */
function changeSharingURL(url) {
    $('.addthis_toolbox a').each(function () {
        $(this).attr('addthis:url', url.toString());
    });
    $(".plusone div").remove();
    gapi.plusone.render("plusone", { "size": "medium", "href": url.toString() });

    addthis.toolbox('.addthis_toolbox');
}
/* </SPUTNIK Change sharing URL> */

var widgets = null;

function getInTouch() {
    // If we're between demos, close the switch UI and restart the current demo.
    if (isSwitchDemoOpen()) {
        switchDemoUI.hide();
        restartDemo();
    }

    if ($('div.earthshim.form').length == 0) {
        var $holder;
        if ($("#demoMap").length > 0) { // banking&financial and Telco demos in place
            $holder = $('#map-bg-holder');
        }
        else {
            $holder = $('.map-corners');
        }

        $holder.append('<div class="earthshim form" id="get_in_touch_form"><iframe id="registration-form" src="http://lp.google-mkto.com/APACGEODiscoverWeb_Form1.html" scrolling="no" width="563" height="586" frameborder="0"></iframe></div>');


        if ($("#demoholder").html().length > 0) {
            $('.earthshim.form').show();
            $('.earthshim').show();
            widgets = $(".ui-widget:visible").hide();
        } else {
            $('.earthshim.form').fadeTo(500, 0.97);
            $('.earthshim').fadeTo(500, 0.97);
        }
    }
    else {
        if ($("#demoholder").html().length > 0) {
            $('.earthshim.form').show();
            $('.earthshim').show();
            widgets = $(".ui-widget:visible").hide();
        } else {
            $('div.earthshim.form').fadeIn(500);
            $('.earthshim').fadeIn(500);
        }
    }
    _gaq.push(function () {
        var pageTracker = _gat._getTrackerByName();
        var iframe = $('#registration-form');
        var allVars = $.getUrlVars();

        var marketoUrl = "http://lp.google-mkto.com/APACGEODiscoverWeb_Form1.html";
        if (language == "zh-tw") {
            marketoUrl = "http://lp.google-mkto.com/tccontactus.html";
        } else if (language == "euc-kr") {
            marketoUrl = "http://lp.google-mkto.com/APACKOGeoDiscoverWebSite_tccontactus.html";
        }

        if ($.getUrlVar('utm_source') != undefined) {
            marketoUrl += "?utm_source=" + $.getUrlVar('utm_source');
            if ($.getUrlVar('utm_medium') != undefined) {
                marketoUrl += "&utm_medium=" + $.getUrlVar('utm_medium');
            }
            if ($.getUrlVar('utm_term') != undefined) {
                marketoUrl += "&utm_term" + $.getUrlVar('utm_term');
            }
            if ($.getUrlVar('utm_content') != undefined) {
                marketoUrl += "&utm_content" + $.getUrlVar('utm_content');
            }
            if ($.getUrlVar('utm_campaign') != undefined) {
                marketoUrl += "&utm_campaign" + $.getUrlVar('utm_campaign');
            }
        }

        iframe.attr('src', pageTracker._getLinkerUrl(marketoUrl));
    });
}

var ge;
var map;
var tiles = [];
var motionInterval = 0;
function init() {
    var start = new google.maps.LatLng(0, 0);
    var located = false;
    if (google.loader.ClientLocation) {
        located = true;
        start = new google.maps.LatLng(google.loader.ClientLocation.latitude, google.loader.ClientLocation.longitude);
    }
    var myOptions = {
        zoom: 5,
        center: start,
        disableDefaultUI: true,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        scrollwheel: false,
        disableDoubleClickZoom: true
    };
    map = new google.maps.Map(document.getElementById('map'), myOptions);
    map.setMapTypeId(google.maps.MapTypeId.SATELLITE);
    google.maps.event.addListenerOnce(map, 'tilesloaded', function () {
        $(".main-info div.default, .verticals, .grid").delay(1000).fadeIn(1000);
        var ieOld = $(".lt-ie9").length > 0;
        if (!ieOld) {
            for (var i = 0; i < tiles.length; i++) {
                $(tiles[i].el.node).delay(1000).delay(Math.floor(Math.random() * 1250)).animate({ 'opacity': 0.55 }, 500);
            }
        }
        setTimeout(function () { $(".main-info div").vAlign(); }, 1100);

    });
    if (!located) {
        if (navigator.geolocation) {
            browserSupportFlag = true;
            navigator.geolocation.getCurrentPosition(function (position) {
                start = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
                map.panTo(start);
            }, function (error) {
                // no need to do anything
            });
            // Try Google Gears Geolocation
        } else if (google.gears) {
            browserSupportFlag = true;
            var geo = google.gears.factory.create('beta.geolocation');
            geo.getCurrentPosition(function (position) {
                start = new google.maps.LatLng(position.latitude, position.longitude);
                map.panTo(start);
            }, function (error) {
                // no need to do anything
            });
        }
    }
    var x = 0;
    var dir = 1;
    var moveInterval = "";

    var moveX = 0;
    var moveY = 0;
    var scale = 2;

    // pan the map in a random direction for 2 seconds, then pause for 8 seconds and repeat.
    motionInterval = setInterval(function () {
        var moving = false;
        moveInterval = window.setInterval(function () {
            if (!moving) {
                moveX = Math.random() * (Math.random() < 0.5 ? -1 : 1);
                moveY = Math.random() * (Math.random() < 0.5 ? -1 : 1);
                moving = true;
            }
            map.panBy(moveX * scale, moveY * scale);
            var bounds = map.getBounds();
            if (bounds.getSouthWest().lat() < -60 || bounds.getNorthEast().lat() > 75) {
                moveY = moveY * -1;
            }
        }, 15);
        setTimeout(function () { clearInterval(moveInterval); }, 3000); // stop moving the map after 3 seconds
    }, 8000);
}


google.setOnLoadCallback(function () {
    // NAVAGIS added hash functionality.
    if (hash != null) {
        $(".map-holder").show();
        $(".map-bottom-shadow").show();
        $("#map").show();
        $(".main-info div").vAlign();
        loadFromHash();
    } else {
        $(".map-holder").hide().slideDown(500);
        $(".map-bottom-shadow").fadeIn(500);
        $("#map").hide().fadeIn(500);
        init();
    }
});

$(function () {

    $('.getintouch').live('click', function () { getInTouch(); });
    $('#languageSelect').click(function () {
        $(this).toggleClass('ui-down');
    }).hover(function () {
        $(this).addClass('ui-hover');
    }, function () {
        $(this).removeClass('ui-hover');
    });

    $('.map-bg-holder').live('click', function () {
        // Do not intercept switch UI events.
        if (isSwitchDemoOpen()) {
            return false;
        }

        // Added $('#get_in_touch_form').remove(); because it still shows up on demo launch without removal.
        if ($("#demoholder").html().length > 0) {
            $('div.earthshim.form').hide();
            $('div.earthshim').hide();
            $('.earthshim').hide();
            $('#get_in_touch_form').remove();
            if (widgets != null) widgets.show();
        } else {
            $('div.earthshim.form').fadeOut(500);
            $('div.earthshim').fadeOut(500);
            $('.earthshim').fadeOut(500, function () { $('#get_in_touch_form').remove(); });
        }

    });

    $(".main-info div").hide();
    $(".controls .road").click(function () {
        map.setMapTypeId(google.maps.MapTypeId.ROADMAP);
        $(".controls .satellite").removeClass("satelliteon").addClass("blue");
        $(".controls .road").addClass("roadon").addClass("blue");
        $(".grid").addClass("gridblue");
        for (var i = 0; i < tiles.length; i++) {
            var tile = tiles[i];
            if (tile.type == "rect") {
                tile.el.attr({ "fill": "90-#192f52-#537bbb" }); $(tile.el.node).css('opacity', .45);
            }
        }
    });
    $(".controls .satellite").click(function () {
        map.setMapTypeId(google.maps.MapTypeId.SATELLITE);
        $(".controls .satellite").addClass("satelliteon").removeClass("blue");
        $(".controls .road").removeClass("roadon").removeClass("blue");
        $(".grid").removeClass("gridblue");
        for (var i = 0; i < tiles.length; i++) {
            var tile = tiles[i];
            if (tile.type == "rect") {
                tile.el.attr({ "fill": "90-#c0c0c0-#FFFFFF" }); $(tile.el.node).css('opacity', .55);
            }
        }
    });
    var tilearray = [[0, 1, 2, 3, 5, 6, 7], [2, 4, 6], [3, 5, 7], [0, 1, 3, 4, 5]];

    var R = Raphael("tiles", 1125, 561);

    var ieOld = $(".lt-ie9").length > 0;

    for (var i = 0; i < tilearray.length; i++) {
        for (var j = 0; j < tilearray[i].length; j++) {
            var tile = R.rect(5 + (141 * tilearray[i][j]), 5 + (140 * i), 132, 132, 3).attr({ "fill": "90-#c0c0c0-#FFFFFF", "stroke": "none" });
            if (ieOld) {
                $(tile.node).css('opacity', 0.55);
            } else {
                $(tile.node).css('opacity', 0);
            }
            tiles.push({ "type": "rect", "el": tile });
        }
    }
    var infoTile = R.rect(5, 145, 273, 273, 6).attr({ fill: "90-#c0c0c0-#FFFFFF", "fill-opacity": .9, stroke: "none" });

    function addTile(x, y, imgSrc, sector) {
        var tile1 = R.set();
        tile1.front = R.image(imgSrc, x, y, 132, 132).transform("s1,1");
        tile1.back = R.image(imgSrc.replace(".png", "flip.png"), x, y, 132, 132).transform("s-1,1r-5").attr({ "opacity": 0 });
        tile1.rect = R.rect(x, y, 132, 132, 3).attr({ fill: "#FFFFFF", "fill-opacity": 0, stroke: "none", "cursor": "pointer", "sector": "sector" }).mouseover(function () { showTile(tile1); }).mouseout(function () { hideTide(tile1); }).click(function () { loadVertical(tile1); });
        tile1.sector = sector;
        tile1.push(
            tile1.front,
            tile1.back,
            tile1.rect
            );
        tiles.push({ "type": "set", "el": tile1 });
    }


    if (language == undefined) {
        addTile(428, 145, "img/tiles/telco.png", "telco");
        addTile(710, 145, "img/tiles/utilities.png", "utilities");
        addTile(287, 285, "img/tiles/resource.png", "resource");
        addTile(569, 285, "img/tiles/government.png", "government");
        addTile(851, 285, "img/tiles/financial.png", "financial");
    }
    else {
        addTile(428, 145, "img/" + language + "/tiles/telco.png", "telco");
        addTile(710, 145, "img/" + language + "/tiles/utilities.png", "utilities");
        addTile(287, 285, "img/" + language + "/tiles/resource.png", "resource");
        addTile(569, 285, "img/" + language + "/tiles/government.png", "government");
        addTile(851, 285, "img/" + language + "/tiles/financial.png", "financial");
    }



    var E = Raphael("earth", 1146, 612);
    var earth = E.image("img/earth.jpg", -133.5, -116.5, 1413, 845);
    earth.transform("s10,10r15").attr({ "opacity": 0 }).hide();

    function showTile(tile) {
        trackTile("mouseover", tile.sector);
        $(".main-info div:visible").stop().fadeOut(500, function () {
            $(".main-info div." + tile.sector).stop().hide().css({ opacity: 1 }).fadeIn(500);
        });
        tile.back.stop().animate(Raphael.animation({ "opacity": 1 }, 0).delay(500)).animate({ transform: "s1,1r0" }, 1000, "<>");
        tile.front.stop().animate(Raphael.animation({ "opacity": 0 }, 0).delay(500)).animate({ transform: "s-1,1r5" }, 1000, "<>");
    }
    function hideTide(tile) {
        tile.back.stop().animate(Raphael.animation({ "opacity": 0 }, 0).delay(500)).animate({ transform: "s-1,1r-5" }, 1000, "<>");
        tile.front.stop().animate(Raphael.animation({ "opacity": 1 }, 0).delay(500)).animate({ transform: "s1,1" }, 1000, "<>");
        $(".main-info div:visible").stop().fadeOut(500, function () {
            setTimeout(function () { $(".main-info div.default").stop().hide().css({ opacity: 1 }).fadeIn(500); }, 10);
        });
    }
    function loadVertical(tile) {
        var ieOld = $(".lt-ie9").length > 0;
        trackTile("click", tile.sector);
        for (var i = 0; i < tiles.length; i++) {
            if (ieOld) {
                ieOldFadeOut(tiles[i], Math.floor(Math.random() * 1000), Math.floor(Math.random() * 1500), tile);
            } else {
                fadeOut(tiles[i], Math.floor(Math.random() * 1000), Math.floor(Math.random() * 1500), tile);
            }
        }
        $(".grid").fadeOut(500);
        if (ieOld) {

            $(".main-info, .controls").fadeOut(1500);
            infoTile.stop().animate(Raphael.animation({ transform: "s0,1r0" }, 2000, "<>", function () {
                infoTile.remove();
            }));

            if (tile.front.attr("opacity") <= 0) {
                tile.rect.remove();
                tile.front.remove();
                tile.back.stop().animate(Raphael.animation({ transform: "s0,1r0" }, 500, "<>", function () {
                    tile.back.remove();
                }));
            } else {
                tile.back.remove();
                tile.rect.remove();
                tile.front.stop().animate(Raphael.animation({ transform: "s0,1r0" }, 500, "<>", function () {
                    tile.front.remove();
                }));
            }
        } else {

            $(".controls").fadeOut(1500);
            $(".main-info").fadeOut(500, function () {
                infoTile.stop().animate(Raphael.animation({ transform: "s-1,1r0" }, 1000, "<>"));
            });
            $(infoTile.node).animate({
                "opacity": 0
            }, 1500, function () {
                infoTile.remove();
            });
            $(tile.back.node).animate({
                opacity: 0
            }, 500, function () {
                tile.back.remove();
                tile.front.remove();
                tile.rect.remove();
            });
        }
        clearInterval(motionInterval);
        if (ieOld) {
            $("#earth").show();
            earth.show().transform("s1,1").animate(Raphael.animation({ "opacity": 1 }, 1000, ">").delay(2000));
        } else {
            earth.show().animate({ transform: "s1,1", "opacity": 1 }, 2500, ">");
        }
        setTimeout(function () { $("#get_in_touch_form").hide(); $(".earthshim").show(); $(".verticals").hide(); loadDemo(tile.sector); }, 3000);
    }

    function mapZoom(zoom) {
        if (zoom > 3) {
            setTimeout(function () {
                mapZoom(zoom - 1);
            }, 500);
            map.setZoom(zoom - 1);
        }
        if (zoom == 3) {
            map.panTo(new google.maps.LatLng(-34.397, 150.644));
        }
    }
    function ieOldFadeOut(tile, delay, fadeDelay, selected) {
        if (tile.type == "rect") {
            tile.el.stop().animate(Raphael.animation({ transform: "s0,1r0", opacity: 0 }, 1500, "<>", function () {
                tile.el.remove();
            }).delay(delay));
        } else {
            if (tile.el.sector != selected.sector) {
                tile.el.rect.remove();
                tile.el.back.remove();
                setTimeout(function () {
                    tile.el.front.stop().animate(Raphael.animation({ "opacity": 0 }, 0).delay(750)).animate({ transform: "s0,1r2.5" }, 1500, "<>", function () {
                        tile.el.front.remove();
                    });
                }, delay);
            }
        }
    }
    function fadeOut(tile, delay, fadeDelay, selected) {
        if (tile.type == "rect") {
            tile.el.stop().animate(Raphael.animation({ transform: "s-1,1r0" }, 1500, "<>").delay(delay));
            setTimeout(function () {
                $(tile.el.node).animate({
                    opacity: 0
                }, 500, function () {
                    tile.el.remove();
                });
            }, fadeDelay);
        } else {
            if (tile.el.sector != selected.sector) {
                tile.el.rect.remove();
                tile.el.back.remove();
                setTimeout(function () {
                    tile.el.front.stop().animate(Raphael.animation({ "opacity": 0 }, 0).delay(750)).animate({ transform: "s0,1r2.5" }, 1500, "<>");
                }, delay);
                setTimeout(function () {
                    $(tile.el.front.node).animate({
                        opacity: 0
                    }, 500, function () {
                        tile.el.front.remove();
                    });
                }, fadeDelay);
            }
        }
    }

    R.renderfix();
});

(function ($) {
    $.fn.vAlign = function () {
        return this.each(function (i) {
            var h = $(this).height();
            var oh = $(this).outerHeight();
            var mt = (h + (oh - h)) / 2;
            $(this).css("margin-top", "-" + mt + "px");
            $(this).css("top", "50%");
            $(this).css("position", "absolute");
        });
    };
})(jQuery);

$.extend({
    getUrlVars: function () {
        var vars = [], hash;
        var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
        for (var i = 0; i < hashes.length; i++) {
            hash = hashes[i].split('=');
            vars.push(hash[0]);
            vars[hash[0]] = hash[1];
        }
        return vars;
    },
    getUrlVar: function (name) {
        return $.getUrlVars()[name];
    }
});